package io.renren.modules.bus.controller;

import java.util.*;

import io.renren.modules.bus.vo.OptometryRecordVO;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.bus.entity.OptometryRecordEntity;
import io.renren.modules.bus.service.OptometryRecordService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 验光记录
 * @Author lililiang
 * @Date 2019-02-23 15:07:23
 */
@RestController
@Api(tags = "验光记录")
@RequestMapping("/bus/optometryrecord")
public class OptometryRecordController extends AbstractController{
    @Autowired
    private OptometryRecordService optometryRecordService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", response = OptometryRecordEntity.class)
    @GetMapping("/list")
    @RequiresPermissions("bus:optometryrecord:list")
    public R list(
            @ApiParam(value = "id", required = false) @RequestParam(required = false) Integer id,
            @ApiParam(value = "会员", required = false) @RequestParam(required = false) String memberName,
            @ApiParam(value = "配镜时间", required = false) @RequestParam(required = false) Date glassesTime,
            @ApiParam(value = "左右（L：左；R：右）", required = false) @RequestParam(required = false) String leftRight,
            @ApiParam(value = "球镜", required = false) @RequestParam(required = false) String sph,
            @ApiParam(value = "柱镜", required = false) @RequestParam(required = false) String columnMirror,
            @ApiParam(value = "轴位", required = false) @RequestParam(required = false) String axial,
            @ApiParam(value = "瞳距", required = false) @RequestParam(required = false) String pd,
            @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "创建人", required = false) @RequestParam(required = false) String authorName,
            @ApiParam(value = "创建时间", required = false) @RequestParam(required = false) Date createTime,
            @ApiParam(value = "修改人", required = false) @RequestParam(required = false) Integer modifier,
            @ApiParam(value = "修改时间", required = false) @RequestParam(required = false) Date modifyTime,
            @ApiParam(value = "状态，1有效，0无效，-1删除", required = false) @RequestParam(required = false) Integer status
        ){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("memberName", memberName);
        params.put("glassesTime", glassesTime);
        params.put("leftRight", leftRight);
        params.put("sph", sph);
        params.put("columnMirror", columnMirror);
        params.put("axial", axial);
        params.put("pd", pd);
        params.put("remark", remark);
        params.put("authorName", authorName);
        params.put("createTime", createTime);
        params.put("modifier", modifier);
        params.put("modifyTime", modifyTime);
        params.put("status", status);
        PageUtils page = optometryRecordService.list4Page(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表查询
     */
    @ApiOperation(value = "列表查询", response = OptometryRecordEntity.class)
    @GetMapping("/listByCondition")
    @RequiresPermissions("bus:optometryrecord:list")
    public R listByCondition(
            @ApiParam(value = "id", required = false) @RequestParam(required = false) Integer id,
                @ApiParam(value = "会员Id", required = false) @RequestParam(required = false) Integer memberId,
                @ApiParam(value = "配镜时间", required = false) @RequestParam(required = false) Date glassesTime,
                @ApiParam(value = "左右（L：左；R：右）", required = false) @RequestParam(required = false) String leftRight,
                @ApiParam(value = "球镜", required = false) @RequestParam(required = false) String sph,
                @ApiParam(value = "柱镜", required = false) @RequestParam(required = false) String columnMirror,
                @ApiParam(value = "轴位", required = false) @RequestParam(required = false) String axial,
                @ApiParam(value = "瞳距", required = false) @RequestParam(required = false) String pd,
                @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark,
                @ApiParam(value = "创建人", required = false) @RequestParam(required = false) Integer author,
                @ApiParam(value = "创建时间", required = false) @RequestParam(required = false) Date createTime,
                @ApiParam(value = "修改人", required = false) @RequestParam(required = false) Integer modifier,
                @ApiParam(value = "修改时间", required = false) @RequestParam(required = false) Date modifyTime,
                @ApiParam(value = "状态，1有效，0无效，-1删除", required = false) @RequestParam(required = false) Integer status
        ){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("memberId", memberId);
        params.put("glassesTime", glassesTime);
        params.put("leftRight", leftRight);
        params.put("sph", sph);
        params.put("columnMirror", columnMirror);
        params.put("axial", axial);
        params.put("pd", pd);
        params.put("remark", remark);
        params.put("author", author);
        params.put("createTime", createTime);
        params.put("modifier", modifier);
        params.put("modifyTime", modifyTime);
        params.put("status", status);
        List<OptometryRecordEntity> list = optometryRecordService.listByCondition(params);
        return R.ok().put("list", list);
    }

    /**
     * 查询详情
     */
    @ApiOperation(value = "查询详情", response = OptometryRecordEntity.class)
    @GetMapping("/info/{id}")
    @RequiresPermissions("bus:optometryrecord:info")
    public R info(@PathVariable("id") Integer id){
        List<OptometryRecordEntity> optometryRecords = optometryRecordService.queryOneByIds(id, id + 1);
        OptometryRecordVO optometryRecordVO = new OptometryRecordVO();
        OptometryRecordEntity optometryRecord = null;
        for (int i = 0; i < optometryRecords.size(); i++) {
            optometryRecord = optometryRecords.get(i);
            optometryRecordVO.setMemberId(optometryRecord.getMemberId());
            optometryRecordVO.setMemberName(optometryRecord.getMemberName());
            optometryRecordVO.setGlassesTime(optometryRecord.getGlassesTime());
            optometryRecordVO.setRemark(optometryRecord.getRemark());
            String leftRight = optometryRecord.getLeftRight();
            //1：左眼；2：右眼
            if ("1".equals(leftRight)) {
                optometryRecordVO.setId_l(optometryRecord.getId());
                optometryRecordVO.setSph_l(optometryRecord.getSph());
                optometryRecordVO.setColumnMirror_l(optometryRecord.getColumnMirror());
                optometryRecordVO.setAxial_l(optometryRecord.getAxial());
                optometryRecordVO.setPd_l(optometryRecord.getPd());
            } else if ("2".equals(leftRight)) {
                optometryRecordVO.setId_r(optometryRecord.getId());
                optometryRecordVO.setSph_r(optometryRecord.getSph());
                optometryRecordVO.setColumnMirror_r(optometryRecord.getColumnMirror());
                optometryRecordVO.setAxial_r(optometryRecord.getAxial());
                optometryRecordVO.setPd_r(optometryRecord.getPd());
            }
        }
        return R.ok().put("optometryRecord", optometryRecordVO);
    }

    /**
     * 新增保存
     */
    @ApiOperation(value = "新增保存", response = R.class)
    @PostMapping("/save")
    @RequiresPermissions("bus:optometryrecord:save")
    public R save(@RequestBody OptometryRecordVO vo){
        OptometryRecordEntity entityL = new OptometryRecordEntity();
        entityL.setMemberId(vo.getMemberId());
        entityL.setGlassesTime(vo.getGlassesTime());
        entityL.setLeftRight("1");
        entityL.setSph(vo.getSph_l());
        entityL.setColumnMirror(vo.getColumnMirror_l());
        entityL.setAxial(vo.getAxial_l());
        entityL.setPd(vo.getPd_l());
        entityL.setRemark(vo.getRemark());
        entityL.setAuthor(this.getUserId().intValue());
        optometryRecordService.insert(entityL);

        OptometryRecordEntity entityR = new OptometryRecordEntity();
        entityR.setMemberId(vo.getMemberId());
        entityR.setGlassesTime(vo.getGlassesTime());
        entityR.setLeftRight("2");
        entityR.setSph(vo.getSph_r());
        entityR.setColumnMirror(vo.getColumnMirror_r());
        entityR.setAxial(vo.getAxial_r());
        entityR.setPd(vo.getPd_r());
        entityR.setRemark(vo.getRemark());
        entityR.setAuthor(this.getUserId().intValue());
        optometryRecordService.insert(entityR);
        return R.ok();
    }

    /**
     * 修改保存
     */
    @ApiOperation(value = "修改保存", response = R.class)
    @PostMapping("/update")
    @RequiresPermissions("bus:optometryrecord:update")
    public R update(@RequestBody OptometryRecordVO vo){
        OptometryRecordEntity entityL = new OptometryRecordEntity();
        entityL.setId(vo.getId_l());
        entityL.setMemberId(vo.getMemberId());
        entityL.setGlassesTime(vo.getGlassesTime());
        entityL.setLeftRight("1");
        entityL.setSph(vo.getSph_l());
        entityL.setColumnMirror(vo.getColumnMirror_l());
        entityL.setAxial(vo.getAxial_l());
        entityL.setPd(vo.getPd_l());
        entityL.setRemark(vo.getRemark());
        entityL.setModifier(this.getUserId().intValue());
        optometryRecordService.updateById(entityL);

        OptometryRecordEntity entityR = new OptometryRecordEntity();
        entityR.setId(vo.getId_r());
        entityR.setMemberId(vo.getMemberId());
        entityR.setGlassesTime(vo.getGlassesTime());
        entityR.setLeftRight("2");
        entityR.setSph(vo.getSph_r());
        entityR.setColumnMirror(vo.getColumnMirror_r());
        entityR.setAxial(vo.getAxial_r());
        entityR.setPd(vo.getPd_r());
        entityR.setRemark(vo.getRemark());
        entityR.setModifier(this.getUserId().intValue());
		optometryRecordService.updateById(entityR);
        return R.ok();
    }

    /**
     * 批量逻辑删除
     */
    @ApiOperation(value = "批量逻辑删除", response = R.class)
    @PostMapping("/delete")
    @RequiresPermissions("bus:optometryrecord:delete")
    public R delete(@RequestBody Integer[] ids){
		optometryRecordService.deleteLogic(Arrays.asList(ids));
        return R.ok();
    }

}
